package com.zhiteng.utils.tencent.campus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 
* <p>Title: SignUtil.java</p>  
* <p>Description: 生成签名(用于腾讯智慧校园API)</p>  
* @author dragon  
* @date 2018年11月9日  
* @version 1.0
 */
public class SignUtil {
	/*public static String getSign(Map<String, String> map) {
		String result = "";
		try {
			List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(map.entrySet());
			// 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
			Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
				public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
					return (o1.getKey()).toString().compareTo(o2.getKey());
				}
			});
			// 构造签名键值对的格式
			StringBuilder sb = new StringBuilder();
			for (Map.Entry<String, String> item : infoIds) {
				if (item.getKey() != null || item.getKey() != "") {
					String key = item.getKey();
					String val = item.getValue();
					if (!(val == "" || val == null)) {
						sb.append(key + "=" + val + "&");
					}
				}
			}
			sb.append("key=377729078cacd0711f00d4df8df260a3");	//拉数据用
			//sb.append("key=507f81351ee5b50350d97f08e6369cd5");	//发消息用
			
			System.out.println("sb:"+sb);
			
			result = sb.toString();
			// 进行MD5加密
			result = DigestUtils.md5Hex(result).toUpperCase();
		} catch (Exception e) {
			return null;
		}
		return result;
	}*/
	
	//	可用于value为Map的Map
	//	注意：拉数据和发消息用的是不同的key
	@SuppressWarnings({ "unchecked" })
	public static String getSign(Map<String, Object> paramMap,String key) {
		String result = "";
		try {
			HashMap<String,Object> map = (HashMap<String,Object>)paramMap;
			HashMap<String, Object> cloneMap = (HashMap<String, Object>) map.clone();
			
			//	构造成data[type]为键的形式
			for (Map.Entry<String, Object> entry : map.entrySet()) {	//	不能在迭代的同时修改，因此clone
				if (entry.getValue() instanceof Map) {
					Map<String,String> innerMap = (Map<String,String>)entry.getValue();
					String outKey = entry.getKey();	// data
					for (Map.Entry<String, String> innerEntry : innerMap.entrySet()) {
						String k = outKey+"["+innerEntry.getKey()+"]";
						String v = innerEntry.getValue();
						cloneMap.put(k, v);
					}
					cloneMap.remove(outKey);
				}
			}
			
			List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(cloneMap.entrySet());
			// 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
			Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
				public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
					return (o1.getKey()).toString().compareTo(o2.getKey());
				}
			});
			// 构造签名键值对的格式
			StringBuilder sb = new StringBuilder();
			for (Map.Entry<String, Object> item : infoIds) {
				if (item.getKey() != null || item.getKey() != "") {
					String k = item.getKey();
					String v = item.getValue().toString();
					if (!(v == "" || v == null)) {
						sb.append(k + "=" + v + "&");
					}
				}
			}
			
			sb.append("key="+key);
			result = sb.toString();
			// 进行MD5加密
			result = DigestUtils.md5Hex(result).toUpperCase();
		} catch (Exception e) {
			return null;
		}
		return result;
	}
	
	
}
